<{include file="public_html/header.html"}>
<{include file="public_html/left_menu.html"}>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/lang/zh-cn/zh-cn.js"></script>
<blockquote class="layui-elem-quote">添加题目</blockquote>

<form class="layui-form layui-form-pane" method="post">
	<input type="hidden" name="id" value="<{$timu_info['id']|default:'0'}>" />
	<div class="layui-form-item">
		<label class="layui-form-label">科目</label>
		<div class="layui-input-block">
			<select name="subject_id" id="subject" lay-filter="subject" lay-verify="required" <{if $timu_info.subject_id|default:0 > 0}> disabled <{/if}>>
				<option value="0">请选择科目</option>
				<{foreach from=$subject item=item}>
					<option value="<{$item.id}>" <{if $timu_info['subject_id']|default:"" eq $item.id}>selected="selected"<{/if}>><{$item.name}></option>
				<{/foreach}>
			</select>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">课本</label>
		<div class="layui-input-block">
			<select name="book_id" id="book" lay-filter="book" lay-verify="required" <{if $timu_info.book_id|default:0 > 0}> disabled <{/if}>>
				<option value="0">请选择课本</option>
				<{foreach from=$book_list item=book}>
					<option value="<{$book.id}>"><{$book.title}></option>
				<{/foreach}>
			</select>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">章节</label>
		<div class="layui-input-block">
			<select name="chapter_id" id="chapter_id" lay-verify="required" <{if $timu_info.book_chapter_id|default:0 > 0}> disabled <{/if}>>
				<option value="0">请选择章节</option>
				<{if $chapter_list_data|count  > 0}>
					<{foreach from=$chapter_list_data item=item}>
						<!-- <optgroup label="<{$item.title}>"> -->
						<option <{if $item.id == $timu_info.chapter_id}> selected <{/if}> value="<{$item.id}>"><{$item.title}></option>
						<{if $item.sub|count > 0}>
							<{foreach from=$item.sub item=item1}>
								<option <{if $item1.id == $timu_info.chapter_id}> selected <{/if}> value="<{$item1.id}>">----<{$item1.title}></option>
							<{/foreach}>
						<{/if}>
						<!-- </optgroup> -->
					<{/foreach}>
				<{/if}>
			</select>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">难度等级</label>
		<div class="layui-input-block">
			<input type="radio" name="difficulty_level" value="1" title="难" <{if $timu_info.difficulty_level|default:"" == 1}> checked <{/if}>>
			<input type="radio" name="difficulty_level" value="2" title="中" <{if $timu_info.difficulty_level|default:"" == 2 || !isset($timu_info)}> checked <{/if}>>
			<input type="radio" name="difficulty_level" value="3" title="易" <{if $timu_info.difficulty_level|default:"" == 3}> checked <{/if}>>
		</div>
	</div>	

	<div class="layui-form-item layui-form-text" id="text_subject_text">
		<label class="layui-form-label">主题目内容</label>
		<div class="layui-input-block">
			<!-- 加载编辑器的容器 -->
		    <script id="container" name="title" type="text/plain">
		        <{$timu_info.title|default:""}>
		    </script>
		    <!-- 配置文件 -->
		    <script type="text/javascript" src="/static/public/ueditor/ueditor.config.js"></script>
		    <!-- 编辑器源码文件 -->
		    <script type="text/javascript" src="/static/public/ueditor/ueditor.all.js"></script>
		    <!-- 实例化编辑器 -->
		    <script type="text/javascript">
		        var ue = UE.getEditor('container');
		    </script>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">题目类型</label>
		<div class="layui-input-block">
			<select name="type" lay-verify="required" lay-filter="timu_type" id="timu_type" <{if $timu_info.type|default:"" > 0}> disabled <{/if}>>
				<option value="0">请选择题型</option>
				<option value="1" <{if 1 == $timu_info.type|default:""}> selected <{/if}>>单选题</option>
				<option value="2" <{if 2 == $timu_info.type|default:""}> selected <{/if}>>多选题</option>
				<option value="3" <{if 3 == $timu_info.type|default:""}> selected <{/if}>>判断题</option>
				<option value="4" <{if 4 == $timu_info.type|default:""}> selected <{/if}>>简答题</option>
				<option value="5" <{if 5 == $timu_info.type|default:""}> selected <{/if}>>填空题</option>
				<option value="6" <{if 6 == $timu_info.type|default:""}> selected <{/if}>>计算题</option>
				<option value="7" <{if 7 == $timu_info.type|default:""}> selected <{/if}>>算式题</option>
				<option value="8" <{if 8 == $timu_info.type|default:""}> selected <{/if}>>图形连线题</option>
				<option value="9" <{if 9 == $timu_info.type|default:""}> selected <{/if}>>看图写答案</option>
				<option value="10" <{if 10 == $timu_info.type|default:""}> selected <{/if}>>看图画星</option>
			</select>
		</div>
	</div>

	<div id="timu_children_box">
		
	</div>
	<div class="layui-input-block">
		<button class="layui-btn" lay-submit lay-filter="sub_topic">保存题目</button>
		<button class="layui-btn" lay-submit lay-filter="sub_topic" name="continue">保存题目并继续添加</button>
	</div>
</form>
<{include file="public_html/footer.html"}>
<script>
	layui.use(['form', 'jquery', 'layedit', 'element'], function(){
		var layedit = layui.layedit,form = layui.form(),$ = layui.jquery, element = layui.element();
		form.on('select(subject)', function(data){
			var ii = layer.load();
			var subject_id = $(data.elem).val();
			$("#book").html('<option value="0">请选择课本</option>');
			form.render('select');
			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_books",
				dataType:"json",
				data:{subject_id:subject_id},
				success: function(result){
					layer.close(ii);
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
							$("#book").append('<option value="'+obj.id+'">'+obj.title+'</option>');
						});
						form.render('select');
					}
				}
			});
		});

		if("<{$timu_info['subject_id']|default:0}>" > 0){
			var subject_id = '<{$timu_info['subject_id']|default:0}>';
			var book_id = '<{$timu_info['book_id']|default:0}>';
			var book_chapter_id = '<{$timu_info['book_chapter_id']|default:0}>';
			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_books",
				dataType:"json",
				data:{subject_id:subject_id},
				success: function(result){
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
							if(obj.id == book_id){
								$("#book").append('<option value="'+obj.id+'" selected>'+obj.title+'</option>');	
							}else{
								$("#book").append('<option value="'+obj.id+'">'+obj.title+'</option>');
							}
							
						});
						form.render('select');
					}
				}
			});

			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_book_chapter",
				dataType:"json",
				data:{book_id:book_id},
				success: function(result){
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
							if(book_chapter_id == obj.id){
								var selected = 'selected';
							}else{
								var selected = false;
							}
                            html += '<option value="'+obj.id+'" '+selected+'>'+obj.title+'</option>';
                            if(obj.sub){
                                $.each(obj.sub, function(j, obj1) {
                                	if(book_chapter_id == obj1.id){
										var selected = 'selected';
									}else{
										var selected = false;
									}
                                	html += '<option value="'+obj1.id+'" '+selected+'>----'+obj1.title+'</option>';
                                });
                            }
						});
						$("#chapter_id").append(html);
						form.render('select');
					}
				}
			});
		}

		form.on('select(book)', function(data){
			var ii = layer.load();
			var book_id = $(data.elem).val();
			$("#chapter_id").html('<option value="0">请选择章节</option>');
			form.render('select');
			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_book_chapter",
				dataType:"json",
				data:{book_id:book_id},
				success: function(result){
					layer.close(ii);
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
                            html += '<option value="'+obj.id+'">'+obj.title+'</option>';
                            if(obj.sub){
                                $.each(obj.sub, function(j, obj1) {
                                	html += '<option value="'+obj1.id+'">----'+obj1.title+'</option>';
                                });
                            }
						});
						$("#chapter_id").append(html);
						form.render('select');
					}
				}
			});
		});
		form.on('select(timu_type)', function(data){
			$("#timu_children_box").html('');
			var ii = layer.load();
			var type = $(data.elem).val();
			var timu_type = new Array();
			timu_type[1] = '/timu/load_danxuan';	//单选题模版
			timu_type[2] = '/timu/load_duoxuan';		//多选题模版
			timu_type[3] = '/timu/load_panduan';		//判断题模版
			timu_type[4] = '/timu/load_jianda';		//简答题模版
			timu_type[5] = '/timu/load_tiankong';	//填空题模版
			timu_type[6] = '/timu/load_jisuan';		//计算题模版
			timu_type[7] = '/timu/load_suanshi';		//算式题模版
			timu_type[8] = '/timu/load_lianxian';	//连线题模版	
			timu_type[9] = '/timu/load_kantixieshu';	//看图写数题模版
			timu_type[10]= '/timu/load_kantuhuaxing';//看图画星题模版
			if(type > 0){
				$("#timu_children_box").load(timu_type[type], function(){
					if($("#container"+type).length > 0){
						UE.delEditor('container'+type);
						UE.getEditor('container'+type);
					}
				});
			}
			layer.close(ii);
		});
	});
</script>